<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use Ixudra\Curl\Facades\Curl;

class MakeCode extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'makes:qrcode';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $user_info = user::orderBy('id','ASC')->get(['openid'])->toArray();
        //var_dump($user_info);exit;
        if($user_info){
            $data = array(
                'keyword1'=>array('value'=>'本宫零食库悄悄开张，内有隐藏剧情','color'=>'#7167ce'),
                'keyword2'=>array('value'=>'本宫实验室神秘研发，全球最有情趣的零食初次预售，还有全场优惠哦','color'=>'#7167ce'),
            );
            $template_id = 'GcDsZ0ALQCGFmPXjVZtSD-3AZNvErBainA18W76_9bA';
            foreach ($user_info as $v){
                $formId = user_formid::lastformid($v['openid']);
                if (!$formId) {
                    savelog('no_push_user', 'openid=' . $v['openid']);
                    continue;
                }
                $res = WxSmallClient::sendTemplate($v['openid'], $data, $formId->form_id, $template_id,'pages/home/home');
                if (!isset($res['errcode']) || $res['errcode'] != 0) {
                    savelog('push_err_log', json_encode($res).'openid='.$v['openid'].'&form_id='.$formId->form_id);
                }
                // 删除用过的form_id
                user_formid::where('id', $formId->id)->delete();
                echo 'ok';
            }
        }
    }
}
